package net.zjitc.controller;

import net.zjitc.pojo.Category;
import net.zjitc.pojo.Product;
import net.zjitc.service.AdminProductService;
import net.zjitc.service.CategoryService;
import net.zjitc.service.impl.AdminProductServiceImpl;
import net.zjitc.service.impl.CategoryServiceImpl;
import net.zjitc.utils.PageBean;
import net.zjitc.utils.dispacher.URLMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Administrator on 2017/12/6.
 */
public class UserProduct {
    CategoryService categoryService = new CategoryServiceImpl();
    AdminProductService adminProductService = new AdminProductServiceImpl();
    int pageSize = 12;
    @URLMapping(url = "/findByCid")
    public String findByCid(HttpServletRequest request, HttpServletResponse response){
        int page = Integer.valueOf(request.getParameter("page"));
        int cid = Integer.valueOf(request.getParameter("cid"));

        List<Category> clist = categoryService.findAll();
        request.setAttribute("clist",clist);

        PageBean<Product> pageBean = adminProductService.findPageBeanByCid(page,pageSize,cid);
        request.setAttribute("page",pageBean);

        request.setAttribute("cid",cid);
        return "WEB-INF/user/productList.jsp";
    }
    @URLMapping(url = "/findByCsid")
    public String findByCsid(HttpServletRequest request, HttpServletResponse response){
        int page = Integer.valueOf(request.getParameter("page"));
        int csid = Integer.valueOf(request.getParameter("csid"));

        List<Category> clist = categoryService.findAll();
        request.setAttribute("clist",clist);

        PageBean<Product> pageBean = adminProductService.findPageBeanByCsid(page,pageSize,csid);
        request.setAttribute("page",pageBean);

        request.setAttribute("csid",csid);
        return "WEB-INF/user/product_secondList.jsp";
    }
    @URLMapping(url = "/findByPid")
    public String findByPid(HttpServletRequest request, HttpServletResponse response){
        int pid = Integer.valueOf(request.getParameter("pid"));
        List<Category> clist = categoryService.findAll();
        request.setAttribute("clist",clist);
        Product product = adminProductService.findBYPid(pid);
        request.setAttribute("p",product);
        return "WEB-INF/user/product.jsp";
    }
}
